<?php

/**
 * Controller_run
 */


class Controller_run extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->file = YOU::loadModel('file');
	}

	/** 首页 **/
	public function actionIndex ()
	{
		$run_lists = $this->db->fetchAll("SELECT * FROM `tbl_run` ORDER BY run_order ASC");
		foreach ($run_lists as $key => $value){
			$run_lists[$key]['run_pic'] = "upload/run_pic/".$value['run_pic'];
		}

		$this->tpl->assign("run_lists", $run_lists);
		$this->tpl->display("run/run.index.tpl.php");

	}

	/** 本地程序列表 **/
	public function actionList ()
	{
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_run`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$run_lists = $this->db->fetchAll("SELECT * FROM `tbl_run` ORDER BY run_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($run_lists as $key => $value){
			$run_lists[$key]['run_pic'] = "upload/run_pic/".$value['run_pic'];
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("nav_active", "admin");
		$this->tpl->assign("menu_active", "run_list");

		$this->tpl->assign("run_lists", $run_lists);
		$this->tpl->display("run/run.list.tpl.php");
	}

	/** 顺序排序 **/
	public function actionOrder ()
	{
		$run_lists = $this->db->fetchAll("SELECT run_id FROM `tbl_run`");
		foreach ($run_lists as $key => $value){
			$run_id = $value['run_id'];
			if (!empty($_POST["run_order_{$run_id}"])){

				$info = array(
				"run_order" => $_POST["run_order_{$run_id}"],
				);

				$this->db->do_update("tbl_run", $info, "run_id='{$run_id}'");

			}

		}


		// 跳转
		Helper_Client::location("排序成功", "index.php?a=ad.list");
	}

	/** 添加本地程序 **/
	public function actionAdd ()
	{
		if ($_POST){

			$run_name = $this->post->run_name;
			$run_key  = $this->post->run_key;
			$run_path = $this->post->run_path;
			$run_url  = $this->post->run_url;

			$info = array(
			"run_name"    => $run_name,
			"run_key"     => $run_key,
			"run_path"    => $run_path,
			"run_url"     => $run_url,
			"status"      => 1,
			"create_time" => date("Y-m-d H:i:s"),
			"update_time" => "0000-00-00 00:00:00",
			);
			$this->db->do_insert("tbl_run", $info);
			$run_id = $this->db->insert_id();

			// 保存图片
			if (is_uploaded_file($_FILES['run_pic']['tmp_name'])){

				// 测试是否为非法上传
				if (function_exists(finfo_open)){
					$finfo    = finfo_open(FILEINFO_MIME);
					$mimetype = finfo_file($finfo, $_FILES['run_pic']['tmp_name']);
					finfo_close($finfo);

					$ftype = $this->model->file->getMime($mime);
				}else{
					$a = explode(".", $_FILES['run_pic']['name']);
					$ftype = strtolower($a[count($a)-1]);
				}

				if(!$this->model->file->checkType($ftype, array("jpg", "png", "gif",) )){
					Helper_Client::alert("上传文件格式非法");
				}

				$run_pic = "{$run_id}.{$ftype}";
				$pic_path = UPLOAD_PATH."/run_pic/{$run_pic}";

				move_uploaded_file($_FILES['run_pic']['tmp_name'], $pic_path);

				$this->db->do_update("tbl_run", array("run_pic" => $run_pic), "run_id='{$run_id}'");

			}

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=run.list");


		}

		$this->tpl->display("run/run.add.tpl.php");
	}


	/** 修改本地程序 **/
	public function actionUpdate ()
	{
		$run_id = (int)$this->get->run_id;

		$run_data = $this->db->fetchRow("SELECT * FROM `tbl_run` WHERE run_id='{$run_id}'");

		if ($_POST){

			$run_name = $this->post->run_name;
			$run_key  = $this->post->run_key;
			$run_path = $this->post->run_path;
			$run_url  = $this->post->run_url;

			$info = array(
			"run_name"     => $run_name,
			"run_key"      => $run_key,
			"run_path"     => $run_path,
			"run_url"      => $run_url,
			"update_time"  => date("Y-m-d H:i:s"),

			);
			$this->db->do_update("tbl_run", $info, "run_id='{$run_id}'");

			// 保存图片
			if (is_uploaded_file($_FILES['run_pic']['tmp_name']) && $this->post->run_pic_re=='1'){

				// 测试是否为非法上传
				if (function_exists(finfo_open)){
					$finfo    = finfo_open(FILEINFO_MIME);
					$mimetype = finfo_file($finfo, $_FILES['run_pic']['tmp_name']);
					finfo_close($finfo);

					$ftype = $this->model->file->getMime($mime);
				}else{
					$a = explode(".", $_FILES['run_pic']['name']);
					$ftype = strtolower($a[count($a)-1]);
				}

				if(!$this->model->file->checkType($ftype, array("jpg", "png", "gif",) )){
					Helper_Client::alert("上传文件格式非法");
				}

				$run_pic = "{$run_id}.{$ftype}";
				$pic_path = UPLOAD_PATH."/run_pic/{$run_pic}";

				move_uploaded_file($_FILES['run_pic']['tmp_name'], $pic_path);

				$this->db->do_update("tbl_run", array("run_pic" => $run_pic), "run_id='{$run_id}'");

			}

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=run.list");

		}

		$this->tpl->assign("run_data", $run_data);
		$this->tpl->display("run/run.update.tpl.php");
	}


	/** 删除本地程序 **/
	public function actionDel ()
	{
		$run_id = (int)$this->get->run_id;

		$sql = "DELETE FROM `tbl_run` WHERE run_id='{$run_id}'";
		$this->db->query($sql);

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=run.list");
	}

	/** 下载注册表文件 **/
	public function actionReg ()
	{
		// 程序列表
		$run_lists = $this->db->fetchAll("SELECT * FROM `tbl_run`");

		$reg = "Windows Registry Editor Version 5.00 \n";
		// 输出protocol列表
		foreach ($run_lists as $key => $value){
			if (empty($value['run_key'])){
				continue;
			}

			$reg .= '
[HKEY_CLASSES_ROOT\''.$value['run_key'].']
@="'.$value['run_key'].' Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\''.$value['run_key'].'\DefaultIcon]
@="'.$value['run_path'].'"

[HKEY_CLASSES_ROOT\''.$value['run_key'].'\shell]
@=""

[HKEY_CLASSES_ROOT\''.$value['run_key'].'\shell\open]
@=""

[HKEY_CLASSES_ROOT\''.$value['run_key'].'\shell\open\command]
@="\"'.$value['run_path'].'\" "
			
			
			'."\n";
		}

		// 下载reg
		// 输出下载文件头
		$fname = "schoolrun.txt";
		header("Content-Disposition: attachment; filename={$fname}");

		echo $reg;
		exit();

	}


}



?>